package 力扣;

/**
 * @author yyq
 * @create 2022-06-09 19:55
 */
public class leetcode135 {
    public int candy(int[] ratings) {
        if(ratings.length==1) return 1;
        int pre=0;
        int sum=0;
        if(ratings[0]>ratings[1]){
            pre = 2;

        }
        else {
            pre = 1;

        }
        sum = pre;
        for (int i =1;i<ratings.length;i++){

            if(ratings[i]>ratings[i-1]){
                sum = sum + pre +1;
                pre = pre +1;
            }
            if(ratings[i]<=ratings[i-1]){
                if(pre-1>=1){
                    sum = sum +pre -1;
                    pre = pre - 1;
                }
                else if(pre-1==0){

                }
                else {
                    sum = sum +1;
                    pre = 1;
                }

            }
        }
        return sum;
    }
}
